﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using SmartMD.Manager.Common;
using Techstar.Dialog;
using Duoease.ContainerModel;
using Duoease.ContainerModel.Entity;
using SmartMD.DataModel;

namespace SmartMD.Manager.Container
{
    /// <summary>
    /// Interaction logic for NewContainerModelDialog.xaml
    /// </summary>
    public partial class NewContainerModelDialog : Window
    {
        public NewContainerModelDialog()
        {
            InitializeComponent();
        }

        public ContainerModelPanel Owner;
        public bool IsFirstCreate = true;
        private bool isPriviewed = false;

        private void btnCreateModel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.Owner == null)
                    return;

                this.Owner.CM.RefreshAllLabel();

                if (txtName.Text.Trim() == "")
                {
                    MessageBox.Show("名称不能为空！", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                if (cmbModelType.Text!="液氮罐" && txtRowNum.Text.Trim() == "")
                {
                    MessageBox.Show("行数不能为空！", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                if (cmbModelType.Text != "液氮罐" && txtColumnNum.Text.Trim() == "")
                {
                    MessageBox.Show("列数不能为空！", "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    return;
                }
                isPriviewed = true;
                CreateContainerModel();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        /**
         * 功能：创建容器模型
         * 
         * */
        private void CreateContainerModel()
        {
            switch (cmbElementType.SelectedValue.ToString())
            {
                case "冰箱":
                    BaseModel fridgeModel = null;
                    string modeltype = (cmbModelType.SelectedItem as ComboBoxValueItem).Value.ToString();
                    if (modeltype == "VerticalFridge")
                    {
                        fridgeModel = new VerticalFridge();
                        fridgeModel.Row = int.Parse(this.txtRowNum.Text);
                        fridgeModel.Column = int.Parse(this.txtColumnNum.Text);
                    }
                    else if (modeltype == "HorizontalFridge")
                    {
                        fridgeModel = new HorizontalFridge();
                        fridgeModel.Row = int.Parse(this.txtRowNum.Text);
                        fridgeModel.Column = int.Parse(this.txtColumnNum.Text);
                    }
                    else if (modeltype == "LiquidNitrogenJar")
                    {
                        fridgeModel = new LiquidNitrogenJar();
                    }
        
                    /* if (this.txtHeight.Text != "")
                     {
                         fridgeModel.UnitHeight = int.Parse(this.txtHeight.Text);
                     }
                     if (this.txtWidth.Text != "")
                     {
                         fridgeModel.UnitWidth = int.Parse(this.txtWidth.Text);
                     }*/
                    string colorStr = this.txtFillColor.SelectedColor.ToString();
                    fridgeModel.SurfaceColor = System.Drawing.ColorTranslator.FromHtml(colorStr);
                    fridgeModel.Alpha = int.Parse(this.txtTransparency.Text == "" ? "122" : this.txtTransparency.Text);
                    string borderColorStr = this.txtBorderColor.SelectedColor.ToString();
                    fridgeModel.LineColor = System.Drawing.ColorTranslator.FromHtml(borderColorStr);
                    fridgeModel.LineWidth = int.Parse(this.txtTransparency.Text == "" ? "1" : this.txtBorderWidth.Text);
                    //this.Owner.CM.FridgeWidth = int.Parse(this.txtWidth.Text == "" ? "0" : this.txtWidth.Text);
                    //this.Owner.CM.FridgeHeight = int.Parse(this.txtHeight.Text == "" ? "0" : this.txtHeight.Text);
                    fridgeModel.Name = txtName.Text;
                    fridgeModel.TagName = (cmbModelType.SelectedItem as ComboBoxValueItem).Value.ToString();
                    this.Owner.CM.FridgeModel = fridgeModel;
                    this.Owner.CM.fridgeSpecification = this.txtRowNum.Text + this.txtColumnNum.Text;
                    if (IsFirstCreate)
                    {
                        IsFirstCreate = false;
                        this.txtWidth.Text = this.Owner.CM.FridgeWidth.ToString();
                        this.txtHeight.Text = this.Owner.CM.FridgeHeight.ToString();
                        this.txtWidth.IsEnabled = true;
                        this.txtHeight.IsEnabled = true;
                    }

                    break;
                case "架子":
                    ShelfModel shelfModel = this.Owner.shelfModel;
                    shelfModel.Row = int.Parse(this.txtColumnNum.Text);
                    shelfModel.Column = int.Parse(this.txtRowNum.Text);
                    if (this.txtHeight.Text != "")
                    {
                        shelfModel.UnitHeight = int.Parse(this.txtHeight.Text);
                    }
                    if (this.txtWidth.Text != "")
                    {
                        shelfModel.UnitWidth = int.Parse(this.txtWidth.Text);
                    }
                    colorStr = this.txtFillColor.SelectedColor.ToString();
                    shelfModel.SurfaceColor = System.Drawing.ColorTranslator.FromHtml(colorStr);
                    shelfModel.Alpha = int.Parse(this.txtTransparency.Text == "" ? "122" : this.txtTransparency.Text);
                    borderColorStr = this.txtBorderColor.SelectedColor.ToString();
                    shelfModel.LineColor = System.Drawing.ColorTranslator.FromHtml(borderColorStr);
                    shelfModel.LineWidth = int.Parse(this.txtTransparency.Text == "" ? "1" : this.txtBorderWidth.Text);

                    this.Owner.CM.ShelfModel = shelfModel;
                    break;
                case "盒子":
                    BoxModel boxModel = this.Owner.boxModel;
                    boxModel.Row = int.Parse(this.txtColumnNum.Text);
                    boxModel.Column = int.Parse(this.txtRowNum.Text);
                    if (this.txtHeight.Text != "")
                    {
                        boxModel.UnitHeight = int.Parse(this.txtHeight.Text);
                    }
                    if (this.txtWidth.Text != "")
                    {
                        boxModel.UnitWidth = int.Parse(this.txtWidth.Text);
                    }
                    colorStr = this.txtFillColor.SelectedColor.ToString();
                    boxModel.SurfaceColor = System.Drawing.ColorTranslator.FromHtml(colorStr);
                    boxModel.Alpha = int.Parse(this.txtTransparency.Text == "" ? "122" : this.txtTransparency.Text);
                    borderColorStr = this.txtBorderColor.SelectedColor.ToString();
                    boxModel.LineColor = System.Drawing.ColorTranslator.FromHtml(borderColorStr);
                    boxModel.LineWidth = int.Parse(this.txtTransparency.Text == "" ? "1" : this.txtBorderWidth.Text);

                    this.Owner.CM.BoxModel = boxModel;
                    break;
            }
       
        }

        private void btnClearModel_Click(object sender, RoutedEventArgs e)
        {
            this.Owner.CM.IsCreatNewModel = true;
            this.Owner.CM.RefreshAllLabel();         
        }
        
        private void cmbModelType_Initialized(object sender, EventArgs e)
        {
            List<ComboBoxValueItem> datasource = new List<ComboBoxValueItem>();
            datasource.Add(new ComboBoxValueItem { Name = "立式冰箱", Value = "VerticalFridge" });
            datasource.Add(new ComboBoxValueItem { Name = "卧式冰箱", Value = "HorizontalFridge"});
            datasource.Add(new ComboBoxValueItem { Name = "液氮罐", Value = "LiquidNitrogenJar" });

            (sender as ComboBox).ItemsSource = datasource;
        }

        private void cmbElementType_Initialized(object sender, EventArgs e)
        {
            List<ComboBoxValueItem> datasource = new List<ComboBoxValueItem>();
            datasource.Add(new ComboBoxValueItem { Name = "冰箱", Value = "Fridge" });
            datasource.Add(new ComboBoxValueItem { Name = "架子", Value = "Shelf" });
            datasource.Add(new ComboBoxValueItem { Name = "盒子", Value = "Box" });

            (sender as ComboBox).ItemsSource = datasource;
        }

        private void txtName_Initialized(object sender, EventArgs e)
        {
            List<ComboBoxValueItem> datasource = new List<ComboBoxValueItem>();
            datasource.Add(new ComboBoxValueItem { Name = "4度冰箱", Value = "Fridge4D" });
            datasource.Add(new ComboBoxValueItem { Name = "-20度冰箱立式", Value = "VerticalFridgeV20D" });
            datasource.Add(new ComboBoxValueItem { Name = "-20度冰箱卧式", Value = "HorizontalFridgeV20D" });
            datasource.Add(new ComboBoxValueItem { Name = "-80度冰箱", Value = "FridgeV80D" });
            datasource.Add(new ComboBoxValueItem { Name = "液氮罐", Value = "LiquidNitrogenJar" });
            datasource.Add(new ComboBoxValueItem { Name = "蜡块柜", Value = "WaxBlockCabinet" });
            datasource.Add(new ComboBoxValueItem { Name = "切片柜", Value = "SectionsCabinet" });            

            (sender as ComboBox).ItemsSource = datasource;
        }

        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (!isPriviewed)
            {
                btnCreateModel_Click(sender, e);
            }
            this.Close();
        }

        private void txtWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                this.Owner.CM.FridgeWidth = int.Parse(this.txtWidth.Text == "" ? "0" : this.txtWidth.Text);
                this.Owner.CM.Refresh(ModelStyle.VerticalFridge);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void txtHeight_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                this.Owner.CM.FridgeHeight = int.Parse(this.txtHeight.Text == "" ? "0" : this.txtHeight.Text);
                this.Owner.CM.Refresh(ModelStyle.VerticalFridge);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void cmbModelType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
                ComboBoxItem cbItem = (ComboBoxItem)this.cmbModelType.ItemContainerGenerator.ContainerFromIndex(this.cmbModelType.SelectedIndex);

                if (cbItem == null)
                {
                    return;
                }
                ComboBoxValueItem selectValue = cbItem.Content as ComboBoxValueItem;
                if (selectValue.Name == "液氮罐")
                {
                    txtRowNum.IsEnabled = false;
                    txtColumnNum.IsEnabled = false;
                    txtWidth.IsEnabled = false;
                    txtHeight.IsEnabled = false;
                    txtRowNum.Text = "";
                    txtColumnNum.Text = "";
                    txtColumnNum.Background = new SolidColorBrush(Colors.Gray);
                    txtRowNum.Background = new SolidColorBrush(Colors.Gray);
                    txtWidth.Background = new SolidColorBrush(Colors.Gray);
                    txtHeight.Background = new SolidColorBrush(Colors.Gray);
                }
                else
                {
                    txtRowNum.IsEnabled = true;
                    txtColumnNum.IsEnabled = true;
                    txtWidth.IsEnabled = true;
                    txtHeight.IsEnabled = true;
                    txtColumnNum.Background = new SolidColorBrush(Colors.White);
                    txtRowNum.Background = new SolidColorBrush(Colors.White);
                    txtWidth.Background = new SolidColorBrush(Colors.White);
                    txtHeight.Background = new SolidColorBrush(Colors.White);
                }
        }

    }
}
